package com.mew.user.gold;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mew.common.dc.db.ProfileService;
import com.mew.common.dc.db.UserService;
import com.mew.common.dc.mapper.GoldWinMapper;
import com.mew.common.dc.mapper.ProfileMapper;
import com.mew.common.entity.statis.GoldWin;
import com.mew.common.entity.user.Profile;
import com.mew.common.entity.user.User;
import com.mew.common.rest.gold.GoldRankPlayersMessage;
import com.mew.common.rest.user.SimpleUserBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/goldrank")
public class GoldRankController {

    @Autowired
    private ProfileService profileService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private GoldWinMapper goldWinMapper;

    /**
     *
     * @param type 0 日富翁，1日赢家，3周赢家
     */
    @PostMapping("/richman")
    @ResponseBody
    public GoldRankPlayersMessage rank(@RequestParam("uid")long uid, @RequestParam("type") int type){

        ArrayList<SimpleUserBO> userBOS = new ArrayList<>();
        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
        Date yyyyMMddTime = DateUtil.parse(yyyyMMdd,"yyyyMMdd");

        if (type == 0){
            LambdaQueryWrapper<Profile> wrapper = new LambdaQueryWrapper<>();
            wrapper.orderByDesc(Profile::getGoldNumber).last("limit 50");
            List<Profile> profiles = profileMapper.selectList(wrapper);
            for (Profile profile : profiles) {
                User user = userService.getRedisUserById(profile.getUserId());
                SimpleUserBO userBO = new SimpleUserBO();
                userBO.fromUser(user,profile.getGoldNumber(),profile.getVipLevel());
                userBOS.add(userBO);
            }
        }else if (type == 1){
            LambdaQueryWrapper<GoldWin> wrapper = new LambdaQueryWrapper<GoldWin>()
                    .eq(GoldWin::getAddTime, yyyyMMddTime)
                    .gt(GoldWin::getWin,0)
                    .orderByDesc(GoldWin::getWin)
                    .last("limit 50");
            List<GoldWin> goldWins = goldWinMapper.selectList(wrapper);
            for (GoldWin goldWin : goldWins) {
                Profile profile = profileService.getRedisProfileById(uid);
                User user = userService.getRedisUserById(goldWin.getUserId());
                SimpleUserBO userBO = new SimpleUserBO();
                userBO.fromUser(user,goldWin.getWin(),profile.getVipLevel());
                userBOS.add(userBO);
            }
        }else if (type == 3){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH,-7);
            String yyyyMMdd_7 = DateUtil.format(calendar.getTime(), "yyyyMMdd");
            Date yyyyMMdd_7Time = DateUtil.parse(yyyyMMdd_7,"yyyyMMdd");
            List<GoldWin> goldWins = goldWinMapper.weekRank(yyyyMMdd_7Time,yyyyMMddTime );
            for (GoldWin goldWin : goldWins) {
                Profile profile = profileService.getRedisProfileById(uid);
                User user = userService.getRedisUserById(goldWin.getUserId());
                SimpleUserBO userBO = new SimpleUserBO();
                userBO.fromUser(user,goldWin.getWin(),profile.getVipLevel());
                userBOS.add(userBO);
            }
        }

        GoldWin goldWin = goldWinMapper.selectOne(new LambdaQueryWrapper<GoldWin>()
                .eq(GoldWin::getUserId, uid)
                .eq(GoldWin::getAddTime, yyyyMMddTime)
                .orderByDesc(GoldWin::getId)
                .last("limit 1"));
        GoldRankPlayersMessage message = new GoldRankPlayersMessage();
        message.setGoldNumber(null==goldWin ? 0:goldWin.getWin());
        message.setType(type);
        message.setUserList(userBOS);

        System.out.println(message);
        return message;

    }

    public static void main(String[] args) {
        String yyyyMMdd = DateUtil.format(new Date(), "yyyyMMdd");
        Date yyyyMMddTime = DateUtil.parse(yyyyMMdd,"yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH,-7);
        String yyyyMMdd_7 = DateUtil.format(calendar.getTime(), "yyyyMMdd");
        Date yyyyMMdd_7Time = DateUtil.parse(yyyyMMdd_7,"yyyyMMdd");
        System.out.println(yyyyMMddTime);
        System.out.println(yyyyMMdd_7Time);
    }
}
